<?php
class lib_upload_file extends lib_base{
	public $table_name = "qc_upload_file";
    public $pk = "file_id";

    public function add($data)
    {
        $data['status'] = 1;
        $data['add_time'] = time();
        return $this->create($data);
    }

    public function getList($cond)
    {
        $all = $this->findAll(array($cond),'add_time desc');
        return $all;
    }

    public function softDelete($fileIds,$del=0)
    {
        $fileIds = joinStr($fileIds);
        return $this->act($fileIds,0);
    }

    public function moveGroup($group_id, $fileIds)
    {
        $fileIds = joinStr($fileIds);
        return $this->update(array("{$this->pk} IN ({$fileIds})"),array('group_id'=>$group_id));
    }

    public function get_image($image_id,$path=0)
    {
        $image = $this->findById($image_id);
        $image['file_path'] = $image['file_url'].$image['file_name'];
        if($path) return $image['file_path'];
        return $image;
    }

    public function get_image_url($image_id)
    {
        $info = $this->findById($image_id);
        $file_path = $info['file_url'].$info['file_name'];
        return $file_path;
    }

    public function get_image_url_thumb($image_id)
    {
        $info = $this->findById($image_id);
        $file_path = $info['file_url'].$info['file_name']."_thumb.jpg";
        return $file_path;
    }


    public function get_image_arr($str)
    {
        $photos = explode(',', $str);
        foreach($photos as $v){
            $v2 = $this->get_image($v);
            $image[] = $v2['file_path'];
        }
        return $image;
    }

    public function upload_image($config,$fileInfo)
    {
        $storageObj = $this->checkStorage($config);
        //上传
        $fileMd5 = md5_file($fileInfo['tmp_name']);
        $this->SaveName = $this->buildSaveName($fileInfo);
        $result = $storageObj->upload($this->SaveName, $fileInfo['tmp_name']);
        if($result){
            // $fileDir = if($this->oss_upload) $fileDir = 'upload/'.$this->oss_upload.'/'
            $data = array(
                'fileMd5' => $fileMd5,
                'fileDir' => $result.$this->fileDir,
                'fileName' => $this->fileName,
            );
            // dump($data);exit;
            return $data;
        }
        return false;
        // $data['fileDir'] = $fileDir;
        // $data['fileName'] = $fileName;


    }


    public function checkStorage($config){
		// 存储引擎
		// $config = $config;
		$storage = $config['default'];

		//加载存储模块
		switch($storage){
			case 'local':
				$storageObj = new \lib\Storage\Local();
				break;
			case 'aliyun':
				$oss_ak = $config['engine']['aliyun']['access_key_id'];
				$oss_sk = $config['engine']['aliyun']['access_key_secret'];
				$oss_endpoint = $config['engine']['aliyun']['endpoint'];
				// $oss_endpoint = 'oss-cn-hangzhou.aliyuncs.com';
				$oss_bucket = $config['engine']['aliyun']['bucket'];
                $oss_upload = $config['engine']['aliyun']['upload'];
				$storageObj = new \lib\Storage\Oss($oss_ak, $oss_sk, $oss_endpoint, $oss_bucket, $oss_upload);
				// dump($storageObj);exit;
				break;
			case 'sae':
				$storageObj = new \lib\Storage\Sae($config['engine']['aliyun']['storagename']);
				break;
			default:
				$storageObj = new \lib\Storage\Local();
				break;
        }

        return $storageObj;
    }

    public function buildSaveName($fileInfo,$fileName='')
    {
		$string='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$rand='';
		for($x=0;$x<6;$x++){
			$rand.=substr($string,mt_rand(0,strlen($string)-1),1);
		}

		$dir = date('Y/md', time());
        $ext = pathinfo($fileInfo['name'], PATHINFO_EXTENSION);
        if(!$fileName){
            $fileName = date('YmdHis') . substr(md5($fileInfo['name']), 0, 5)
                . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
        }
        $this->fileDir = $dir;
        $this->fileName = $fileName;
		return $this->fileDir. '/' .$this->fileName;
    }


}